package com.party.core.service.wechat.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.wechat.WechatMaterialReadDao;
import com.party.core.dao.write.wechat.WechatMaterialWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.wechat.WechatMaterial;
import com.party.core.service.wechat.IWechatMaterialService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by admin on 2017/9/4.
 */
@Service
public class WechatMaterialService implements IWechatMaterialService {

    @Autowired
    private WechatMaterialReadDao wechatMaterialReadDao;

    @Autowired
    private WechatMaterialWriteDao wechatMaterialWriteDao;

    @Override
    public String insert(WechatMaterial wechatMaterial) {
        BaseModel.preInsert(wechatMaterial);
        boolean result = this.wechatMaterialWriteDao.insert(wechatMaterial);
        if(result){
            return wechatMaterial.getId();
        }
        return null;
    }

    @Override
    public boolean update(WechatMaterial wechatMaterial) {
        wechatMaterial.setUpdateDate(new Date());
        return this.wechatMaterialWriteDao.update(wechatMaterial);
    }

    @Override
    public boolean deleteLogic(@NotNull String s) {
        return Strings.isNullOrEmpty(s)?false:this.wechatMaterialWriteDao.delete(s);
    }

    @Override
    public boolean delete(@NotNull String s) {
        return Strings.isNullOrEmpty(s)?false:this.wechatMaterialWriteDao.delete(s);
    }

    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> set) {
        return CollectionUtils.isEmpty(set)?false:this.wechatMaterialWriteDao.batchDeleteLogic(set);
    }

    @Override
    public boolean batchDelete(@NotNull Set<String> set) {
        return CollectionUtils.isEmpty(set)?false:this.wechatMaterialWriteDao.batchDelete(set);
    }

    @Override
    public WechatMaterial get(String s) {
        return this.wechatMaterialReadDao.get(s);
    }

    @Override
    public List<WechatMaterial> listPage(WechatMaterial wechatMaterial, Page page) {
        return this.wechatMaterialReadDao.listPage(wechatMaterial,page);
    }

    @Override
    public List<WechatMaterial> list(WechatMaterial wechatMaterial) {
        return this.wechatMaterialReadDao.listPage(wechatMaterial,null);
    }

    @Override
    public List<WechatMaterial> batchList(@NotNull Set<String> set, WechatMaterial wechatMaterial, Page page) {
        return CollectionUtils.isEmpty(set)? Collections.EMPTY_LIST:this.wechatMaterialReadDao.batchList(set,new HashedMap(),page);
    }

    @Override
    @Transactional
    public boolean batchInsert(List<WechatMaterial> list) {
        for(WechatMaterial material: list){
            this.insert(material);
        }
        return true;
    }

    @Override
    public boolean deleteByMemberId(String memberid) {
        return wechatMaterialWriteDao.deleteByMemberId(memberid);
    }
}
